package com.tengfei.blogsystem.dao;

import com.tengfei.blogsystem.pojo.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-16-17:01
 * @description
 */
public interface UserDao extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {
    // 根据用户名查找
    User findOneByuserName(String userName);

    // 根据邮箱查找
    User findOneByEmail(String email);

    // 根据id查找
    User findOneById(String userId);

    // 通过更改用户状态来删除用户
    @Modifying
    @Query(nativeQuery = true, value = "update `tb_user` set `state`='0' where `id`=?")
    int deleteUserById(String userId);

    @Query(value = "select new User(u.id,u.userName,u.roles,u.avatar,u.email,u.sign,u.state,u.regIp,u.loginIp,u.createTime,u.updateTime) from User as u")
    Page<User> listAllUserNoPassword(Pageable pageable);

    @Modifying
    @Query(nativeQuery = true,value = "update `tb_user` set `password`=? where `email`=?")
    int updatePasswordByEmail(String encodePassword, String email);

    @Modifying
    @Query(nativeQuery = true,value = "update `tb_user` set `email`=?,`update_time`=? where `id`=?")
    int updateEmail(String email, Date date, String id);
}
